package cn.iocoder.yudao.module.hs.controller.admin.shipordertrayrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.hs.controller.admin.shipordertrayrecord.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.shipordertrayrecord.ShipOrderTrayRecordDO;
import cn.iocoder.yudao.module.hs.service.shipordertrayrecord.ShipOrderTrayRecordService;

@Tag(name = "管理后台 - 出库单托盘记录")
@RestController
@RequestMapping("/hs/ship-order-tray-record")
@Validated
public class ShipOrderTrayRecordController {

    @Resource
    private ShipOrderTrayRecordService shipOrderTrayRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建出库单托盘记录")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-tray-record:create')")
    public CommonResult<Long> createShipOrderTrayRecord(@Valid @RequestBody ShipOrderTrayRecordSaveReqVO createReqVO) {
        return success(shipOrderTrayRecordService.createShipOrderTrayRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出库单托盘记录")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-tray-record:update')")
    public CommonResult<Boolean> updateShipOrderTrayRecord(@Valid @RequestBody ShipOrderTrayRecordSaveReqVO updateReqVO) {
        shipOrderTrayRecordService.updateShipOrderTrayRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出库单托盘记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:ship-order-tray-record:delete')")
    public CommonResult<Boolean> deleteShipOrderTrayRecord(@RequestParam("id") Long id) {
        shipOrderTrayRecordService.deleteShipOrderTrayRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出库单托盘记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-tray-record:query')")
    public CommonResult<ShipOrderTrayRecordRespVO> getShipOrderTrayRecord(@RequestParam("id") Long id) {
        ShipOrderTrayRecordDO shipOrderTrayRecord = shipOrderTrayRecordService.getShipOrderTrayRecord(id);
        return success(BeanUtils.toBean(shipOrderTrayRecord, ShipOrderTrayRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出库单托盘记录分页")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-tray-record:query')")
    public CommonResult<PageResult<ShipOrderTrayRecordRespVO>> getShipOrderTrayRecordPage(@Valid ShipOrderTrayRecordPageReqVO pageReqVO) {
        PageResult<ShipOrderTrayRecordDO> pageResult = shipOrderTrayRecordService.getShipOrderTrayRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShipOrderTrayRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出库单托盘记录 Excel")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-tray-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShipOrderTrayRecordExcel(@Valid ShipOrderTrayRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShipOrderTrayRecordDO> list = shipOrderTrayRecordService.getShipOrderTrayRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出库单托盘记录.xls", "数据", ShipOrderTrayRecordRespVO.class,
                        BeanUtils.toBean(list, ShipOrderTrayRecordRespVO.class));
    }

}